package com.example.securitymanagement.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class FilmeController {
    @GetMapping("/detail/{type}/{path}")//对外暴露的路径
    public String toDetail(@PathVariable("type") String type, @PathVariable("path") String path) {
        return "detail/" + type + "/" + "path";
    }

    @GetMapping("/userLogin")
    public String toLoginPage() {
        return "/login/login";
    }

    @GetMapping("/getUserByContext")
    @ResponseBody
    public void getUser() {
        SecurityContext context = SecurityContextHolder.getContext();

        //获取用户相关信息
        Authentication authentication = context.getAuthentication();
        UserDetails principal = (UserDetails) authentication.getPrincipal();
        System.out.println(principal.getUsername());
    }
}
